<template>
  <a-modal :title="title" :width="800" :visible="visible" :confirmLoading="confirmLoading" @ok="handleOk"
    @cancel="handleCancel" cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form-model ref='form' :model='model' :rules='validatorRules'>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="模板标题" prop='templateName'>
          <a-input disabled v-model="model.templateName" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="模板内容" prop='templateContent'>
          <a-textarea disabled v-model="model.templateContent" :autosize="{ minRows: 5, maxRows: 8 }" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="数据参数" prop='testData'>
          <a-textarea placeholder="请输入json格式测试数据" v-model="model.testData" :autosize="{ minRows: 5, maxRows: 8 }" />
        </a-form-model-item>
        <!--        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="消息类型">-->
        <!--          <j-dict-select-tag v-model="msgType" placeholder="请选择消息类型" dictCode="msgType" />-->
        <!--        </a-form-model-item>-->
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="消息接收方" required prop='receiver'>
          <j-select-user-by-dep placeholder="请选择消息接收方" store="id" v-model="model.receiver"></j-select-user-by-dep>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import JSelectUserByDep from '@/components/jeecgbiz/JSelectUserByDep'

export default {
  name: "SysMessageTestModal",
  components: {
    JSelectUserByDep
  },
  data() {
    return {
      validatorRules: {
        receiver: [{ required: true, message: '请选择消息接收方' }],
      },
      title: "操作",
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      url: {
        send: "/sys/message/sysMessageTemplate/sendMsg",
      },
      templateName: "",
      templateContent: "",
      receiver: "",
      msgType: "",
      testData: "",
      sendParams: {}
    }
  },
  methods: {
    open(record) {
      this.model = record
      this.visible = true;
    },
    close() {
      this.model = {
        receiver:'',
        testData:'',
      }
      this.receiver = "";
      this.msgType = "";
      this.sendParams = {};
      this.visible = false;
    },
    handleOk() {
      const that = this
      this.$refs.form.validate(valid => {
        if (valid) {
          let httpurl = that.url.send;
          let method = 'post';
          that.sendParams.testData = that.model.testData;
          that.sendParams.receiver = that.model.receiver;
          that.sendParams.templateCode = that.model.templateCode;
          // that.sendParams.msgType = that.msgType;
          that.sendParams.msgType = '4'; // 改造为只支持系统消息
          httpAction(httpurl, that.sendParams, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
            that.close();
          })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    },
  }
}
</script>

<style scoped></style>